//
//  ZTPrivatePushModule.m
//  ZTSDKDemo
//
//  Created by 武得亮 on 2021/5/12.
//

#import "ZTPrivatePushModule.h"

#import "ZTLoadZYHTMLViewController.h"

//#import "GHAllNotesViewController.h"

#import "ZTNoPermissionsViewController.h"
#import "ZTUserInfoManage.h"
//#import "ZGovernmentReadFileViewController.GHChooseOrgListViewController
#import "ZTHttpUrlDefine.h"
#import "ZTPPNetworkHelper.h"
#import "AFHTTPSessionManager.h"
#import "ApiCenter.h"
#import "BaseModel.h"
#import "MJExtension.h"
#import "NSObject+ZTNotNull.h"
#import "UserModel.h"
#import "ApiCenter+Comman.h"
#import "UIViewController+ZTHUD.h"
#import "GHChooseOrgListViewController.h"

@implementation ZTPrivatePushModule

//inspector_has_chosen  督查督办
//transport_management  用车管理
//document_center  文档中心
//news  新闻
//transaction_management  事务管理
//to_do  待办
//announcement  公告
//official_document  公文
//duty_management  值班管理
//assets_management  资产管理
//contract_management  合同管理


/** 跳转政通控制器*/
+(void)pushZTPrivatejsonDict:(NSDictionary *)ztjsonDict andAuth_code:(NSString *)authcode{
    
    
////  -------  测试信息
/// * 保存使用的登录用户账号信息   替换成自己项目中的账号信息  项目登录成功以后共享登录信息  便于其他功能跳转
//    NSDictionary *dict = @{
//        @"userAccount":@"jld2",   //用户账号
//        @"userName":@"jld2",      //用户名
//        @"userPdId":@"10617",     //部门id
//        @"userId":@"46235",       //用户id
//    };
//    [[NSUserDefaults standardUserDefaults]setObject:dict forKey:ZTPrivateUserInfoKey];
//    [[NSUserDefaults standardUserDefaults]synchronize];
//
//    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//    [userDefaults setObject:@"c80cb5bb32155651c1f43d5bb24e784d" forKey:APPDeviceToken];
//    [userDefaults synchronize];

    
    NSString *userName = [[ZTUserInfoManage defaultService]getLoginUserName];
    if (userName.length > 0) {
        [self checkZtprivatePermissAuthcode:authcode ZTPrivatejsonDict:ztjsonDict];
    }else{
        NSString *urlString = [NSString stringWithFormat:@"http://zhbg.zhtchina.net:8280/masCustomer/service/jzbApi/getUserInfo"];
        NSDictionary *paramDic = @{@"deviceId": @"ios",
                                   @"code":authcode
        };
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //    manager.requestSerializer = [AFJSONRequestSerializer serializer];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/xml", @"text/plain", @"application/javascript", @"application/x-www-form-urlencoded", @"image/*", nil];
        //发送请求
        [manager POST:urlString parameters:paramDic headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"输出请求信息 == %@",responseObject);
            NSDictionary *jsonDict = [responseObject mj_JSONObject];
            ////  -------  测试信息
//          NSDictionary *jsonDict =  @{
//                @"msgList": @[
//                    @{
//                        @"pDeptName": @"测试部门",
//                        @"deptFullName": @"generalOffice",
//                        @"deptNodesName": @"北京市>测试单位>测试部门",
//                        @"pdId": @"148695",
//                        @"deptId": @"1813",
//                        @"orgId": @"1813",
//                        @"majorDept": @"1",
//                        @"deptName": @"测试部门",
//                        @"position": @"",
//                        @"telephone": @"",
//                        @"fax": @""
//                }, @{
//                    @"pDeptName": @"测试",
//                    @"deptNodesName": @"北京市>市政府>中移内部测试单位>测试",
//                    @"pdId": @"148799",
//                    @"deptId": @"4699",
//                    @"orgId": @"4699",
//                    @"majorDept": @"0",
//                    @"deptName": @"测试",
//                    @"position": @"",
//                    @"telephone": @"",
//                    @"fax": @""
//                }],
//                @"result": @"success"
//          };
            BaseModel *basemodel = [BaseModel mj_objectWithKeyValues:jsonDict];
            if ([basemodel.msg isNotEmpty]) {
                UserModel *userModel = [UserModel mj_objectWithKeyValues:[jsonDict objectForKey:@"msg"]];
                /** 保存使用的登录用户账号信息   替换成自己项目中的账号信息  项目登录成功以后共享登录信息  便于其他功能跳转*/
                NSDictionary *dict = @{
                    @"userAccount":userModel.account,   //用户账号
                    @"userName":userModel.account,      //用户名
                    @"userPdId":userModel.pdId,     //部门id
                    @"userId":userModel.uid,       //用户id
                };
                [[NSUserDefaults standardUserDefaults]setObject:dict forKey:ZTPrivateUserInfoKey];
                [[NSUserDefaults standardUserDefaults]synchronize];
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                [userDefaults setObject:userModel.token forKey:APPDeviceToken];
                [userDefaults synchronize];
                [self checkZtprivatePermissAuthcode:authcode ZTPrivatejsonDict:ztjsonDict];
            }else{
                if ([basemodel.msgList count] >0) {
                    GHChooseOrgListViewController * org = [GHChooseOrgListViewController new];
                    org.model = basemodel;
                    org.ztjsonDict = ztjsonDict;
                    org.navigationController.navigationBar.hidden = YES;
                    [(UINavigationController *)[[UIApplication sharedApplication]keyWindow].rootViewController pushViewController:org animated:YES];
            }else{
                    [[[[UIApplication sharedApplication]keyWindow] rootViewController] showHUDOnlyText:[jsonDict objectForKey:@"errmsg"]];
                }
            }
            NSLog(@"输出  responseObject == %@",responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [[[[UIApplication sharedApplication]keyWindow] rootViewController] showHUDOnlyText:error.localizedDescription];
        }];
    }
}


#pragma mark  检查是否有权限问题
/**
 @brief
 检查是否有权限问题
 *
 */
+(void)checkZtprivatePermissAuthcode:(NSString *)authcode ZTPrivatejsonDict:(NSDictionary *)ztjsonDict{

    NSString *urlString = [NSString stringWithFormat:@"http://zhbg.zhtchina.net:8280/masCustomer/service/appPrivileges/getAppCenterByCode"];
    NSDictionary *paramDic = @{@"deviceId": @"ios",
                               @"code":@"hand_2",
                               @"pdId":[[ZTUserInfoManage defaultService] getLoginUserPdId],
    };
    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/xml", @"text/plain", @"application/javascript", @"application/x-www-form-urlencoded", @"image/*", nil];

    //发送请求
    [manager POST:urlString parameters:paramDic headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dict = [responseObject mj_JSONObject];
        if ([[dict objectForKey:@"msg"] isNotEmpty]) {
            
            NSMutableArray *msgArray = [dict objectForKey:@"msg"];
            NSMutableArray *msgStringAry = [msgArray valueForKey:@"appName"];


            NSString *ztprivatetype = [ztjsonDict objectForKey:@"type"];
            NSString *titleString = @"";
            if ([ztprivatetype isEqualToString:@"inspector_has_chosen"]) {
                titleString = @"督查督办";
            }else if ([ztprivatetype isEqualToString:@"transport_management"]){
                titleString = @"用车管理";
            }else if ([ztprivatetype isEqualToString:@"document_center"]){
                titleString = @"文档中心";
            }else if ([ztprivatetype isEqualToString:@"news"]){
                titleString = @"新闻";
            }else if ([ztprivatetype isEqualToString:@"transaction_management"]){
                titleString = @"事务管理";
            }else if ([ztprivatetype isEqualToString:@"to_do"]){
                titleString = @"待办";
            }else if ([ztprivatetype isEqualToString:@"announcement"]){
                titleString = @"公告";
            }else if ([ztprivatetype isEqualToString:@"official_document"]){
                titleString = @"公文";
            }else if ([ztprivatetype isEqualToString:@"duty_management"]){
                titleString = @"值班管理";
            }else if ([ztprivatetype isEqualToString:@"assets_management"]){
                titleString = @"资产管理";
            }else if ([ztprivatetype isEqualToString:@"contract_management"]){
                titleString = @"合同管理";
            }
            if ([msgStringAry containsObject:titleString]) {
                [self pushZtprivateWebVc:ztjsonDict];
            }else{
                [self ztnopermisspushVCTitle:titleString];
            }
            
            
        }else{
            [[[[UIApplication sharedApplication]keyWindow] rootViewController] showHUDOnlyText:@"暂无权限"];
        }
        NSLog(@"输出  responseObject == %@",responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [[[[UIApplication sharedApplication]keyWindow] rootViewController] showHUDOnlyText:error.localizedDescription];

    }];



}

+ (void)pushZtprivateWebVc:(NSDictionary *)ztjsonDict{
    
    NSString *ztprivatetype = [ztjsonDict objectForKey:@"type"];
    if ([ztprivatetype isEqualToString:@"inspector_has_chosen"]) {
        NSDictionary *dict = @{
            @"ip":@"http://111.206.218.241:8080",
            @"url":[NSString stringWithFormat:@"http://111.206.218.241:8080/seeyon/m3/apps/v5/biz/html/bizInfo.html?menuId=6660106445562069148&name=%@",[[ZTUserInfoManage defaultService]getLoginUserName]]
        };
        //督查督办
        [ZTPrivatePushModule openSingleH5MethodsWithParaDic:dict];
        
        
    }else if ([ztprivatetype isEqualToString:@"transport_management"]){
        //用车管理
        NSDictionary *dict = @{
            @"ip":@"http://111.206.218.241:8080",
            @"url":[NSString stringWithFormat:@"http://111.206.218.241:8080/seeyon/m3/apps/v5/biz/html/bizInfo.html?menuId=8672116713043215757&name=%@",[[ZTUserInfoManage defaultService]getLoginUserName]],
        };
   
        [ZTPrivatePushModule openSingleH5MethodsWithParaDic:dict];
        
    }else if ([ztprivatetype isEqualToString:@"document_center"]){
       
        NSDictionary *dict = @{
            @"ip":@"http://111.206.218.241:8080",
            @"appCode":@"doc"
        };
        //文档中心
        NSString *ipString;
        if ([dict.allKeys containsObject:@"ip"]) {
            ipString = dict[@"ip"];
        }
        
        ZTLoadZYHTMLViewController * html = [[ZTLoadZYHTMLViewController alloc] init];
       
//        NSString * urlTest = [ipString stringByAppendingFormat:ZYLOGIN_URI, [[ZTUserInfoManage defaultService] getLoginUserAccount]];
        html.hostUrlString =ipString;
        html.url =  [html.hostUrlString stringByAppendingFormat:ZYWENDANGZHONGXIN_URI];
        [(UINavigationController *)[[UIApplication sharedApplication]keyWindow].rootViewController pushViewController:html animated:YES];
        
        
//        ZTWebViewController * html = [[ZTWebViewController alloc] init];
//        html.hostUrlString = ipString;
//        html.urlString = [html.hostUrlString stringByAppendingFormat:ZYWENDANGZHONGXIN_URI];
//        html.title = [dict objectForKey:@"appName"];
//        [(UINavigationController *)[[UIApplication sharedApplication]keyWindow].rootViewController pushViewController:html animated:YES];
        

        
        
    }else if ([ztprivatetype isEqualToString:@"news"]){
        
        NSDictionary *dict = @{
            @"ip":@"http://111.206.218.241:8080",
            @"appCode":@"news"
        };
        //新闻
        NSString *ipString;
        if ([dict.allKeys containsObject:@"ip"]) {
            ipString = dict[@"ip"];
        }
        ZTLoadZYHTMLViewController * html = [[ZTLoadZYHTMLViewController alloc] init];
       
//        NSString * urlTest = [ipString stringByAppendingFormat:ZYLOGIN_URI, [[ZTUserInfoManage defaultService] getLoginUserAccount]];
        html.hostUrlString =ipString;
        html.url = [html.hostUrlString stringByAppendingFormat:ZYREDIRECT_URI];;
        [(UINavigationController *)[[UIApplication sharedApplication]keyWindow].rootViewController pushViewController:html animated:YES];
        
//        ZTWebViewController * html = [[ZTWebViewController alloc] init];
//        html.hostUrlString = ipString;
//    //        html.hostUrlString = @"http://130.120.110.122";
//        html.urlString = [html.hostUrlString stringByAppendingFormat:ZYREDIRECT_URI];
//        html.title = [dict objectForKey:@"appName"];
//        [(UINavigationController *)[[UIApplication sharedApplication]keyWindow].rootViewController pushViewController:html animated:YES];
        
        
    }else if ([ztprivatetype isEqualToString:@"transaction_management"]){
        
        NSDictionary *dict = @{
            @"ip":@"http://111.206.218.241:8080",
            @"appCode":@"collaboration"
        };
        //事务管理
        NSString *ipString;
        if ([dict.allKeys containsObject:@"ip"]) {
            ipString = dict[@"ip"];
        }
        
        ZTLoadZYHTMLViewController * html = [[ZTLoadZYHTMLViewController alloc] init];
       
//        NSString * urlTest = [ipString stringByAppendingFormat:ZYLOGIN_URI, [[ZTUserInfoManage defaultService] getLoginUserAccount]];
        html.hostUrlString =ipString;
        html.url = [html.hostUrlString stringByAppendingFormat:ZYSHIWU_URI];
        [(UINavigationController *)[[UIApplication sharedApplication]keyWindow].rootViewController pushViewController:html animated:YES];
        
        
//        ZTWebViewController * html = [[ZTWebViewController alloc] init];
//        html.hostUrlString = ipString;
//        html.urlString = [html.hostUrlString stringByAppendingFormat:ZYSHIWU_URI];
//        html.title = [dict objectForKey:@"appName"];
//        [(UINavigationController *)[[UIApplication sharedApplication]keyWindow].rootViewController pushViewController:html animated:YES];
        
        
    }else if ([ztprivatetype isEqualToString:@"to_do"]){
        NSDictionary *dict = @{
            @"ip":@"http://111.206.218.241:8080",
            @"appCode":@"daiban"
        };
        //待办
        NSString *ipString;
        if ([dict.allKeys containsObject:@"ip"]) {
            ipString = dict[@"ip"];
        }
        
        ZTLoadZYHTMLViewController * html = [[ZTLoadZYHTMLViewController alloc] init];
       
//        NSString * urlTest = [ipString stringByAppendingFormat:ZYLOGIN_URI, [[ZTUserInfoManage defaultService] getLoginUserAccount]];
        html.hostUrlString =ipString;
        html.url =  [ipString stringByAppendingFormat:ZYDAIBAN_URI];
        [(UINavigationController *)[[UIApplication sharedApplication]keyWindow].rootViewController pushViewController:html animated:YES];
//        ZTWebViewController * html = [[ZTWebViewController alloc] init];
//        html.hostUrlString = ipString;
//        html.urlString = [ipString stringByAppendingFormat:ZYDAIBAN_URI];
//        html.title = [dict objectForKey:@"appName"];
//        [(UINavigationController *)[[UIApplication sharedApplication]keyWindow].rootViewController pushViewController:html animated:YES];
        
        
    }else if ([ztprivatetype isEqualToString:@"announcement"]){
        NSDictionary *dict = @{
            @"ip":@"http://111.206.218.241:8080",
            @"appCode":@"bulletin"
        };
        //公告
        NSString *ipString;
        if ([dict.allKeys containsObject:@"ip"]) {
            ipString = dict[@"ip"];
        }
        ZTLoadZYHTMLViewController * html = [[ZTLoadZYHTMLViewController alloc] init];
       
//        NSString * urlTest = [ipString stringByAppendingFormat:ZYLOGIN_URI, [[ZTUserInfoManage defaultService] getLoginUserAccount]];
        html.hostUrlString =ipString;
        html.url =  [ipString stringByAppendingFormat:ZYGONGGAO_URI];
        [(UINavigationController *)[[UIApplication sharedApplication]keyWindow].rootViewController pushViewController:html animated:YES];
//        ZTWebViewController * html = [[ZTWebViewController alloc] init];
//        html.hostUrlString = ipString;
//        html.urlString = [ipString stringByAppendingFormat:ZYGONGGAO_URI];
//        html.title = [dict objectForKey:@"appName"];
//        [(UINavigationController *)[[UIApplication sharedApplication]keyWindow].rootViewController pushViewController:html animated:YES];
        
        
    }else if ([ztprivatetype isEqualToString:@"official_document"]){
        NSDictionary *dict = @{
            @"ip":@"http://111.206.218.241:8080",
            @"appCode":@"gongwen"
        };
        //公文
        NSString *ipString;
        if ([dict.allKeys containsObject:@"ip"]) {
            ipString = dict[@"ip"];
        }
        
        ZTLoadZYHTMLViewController * html = [[ZTLoadZYHTMLViewController alloc] init];
       
//        NSString * urlTest = [ipString stringByAppendingFormat:ZYLOGIN_URI, [[ZTUserInfoManage defaultService] getLoginUserAccount]];
        html.hostUrlString =ipString;
        html.url =  [html.hostUrlString stringByAppendingFormat:ZYZHENGWEN_URI];
        [(UINavigationController *)[[UIApplication sharedApplication]keyWindow].rootViewController pushViewController:html animated:YES];
        
//        ZTWebViewController * html = [[ZTWebViewController alloc] init];
//        html.hostUrlString = ipString;
//    //        html.hostUrlString = @"http://130.120.110.122";
//        html.urlString = [html.hostUrlString stringByAppendingFormat:ZYZHENGWEN_URI];
//        html.title = [dict objectForKey:@"appName"];
//        [(UINavigationController *)[[UIApplication sharedApplication]keyWindow].rootViewController pushViewController:html animated:YES];
        
        
    }else if ([ztprivatetype isEqualToString:@"duty_management"]){
        NSDictionary *dict = @{
            @"ip":@"http://111.206.218.241:8080",
            @"url":[NSString stringWithFormat:@"http://111.206.218.241:8080/seeyon/m3/apps/v5/biz/html/bizInfo.html?menuId=1517303629883720370&name=%@",[[ZTUserInfoManage defaultService]getLoginUserName]],
        };
        //值班管理
        [ZTPrivatePushModule openSingleH5MethodsWithParaDic:dict];

        
        
    }else if ([ztprivatetype isEqualToString:@"assets_management"]){
        NSDictionary *dict = @{
            @"ip":@"http://111.206.218.241:8080",
            @"url":[NSString stringWithFormat:@"http://111.206.218.241:8080/seeyon/m3/apps/v5/biz/html/bizInfo.html?menuId=1312597677305907543&name=%@",[[ZTUserInfoManage defaultService]getLoginUserName]],
        };
        //资产管理
        [ZTPrivatePushModule openSingleH5MethodsWithParaDic:dict];

        
        
    }else if ([ztprivatetype isEqualToString:@"contract_management"]){
        NSDictionary *dict = @{
            @"ip":@"http://111.206.218.241:8080",
            @"url":[NSString stringWithFormat:@"http://111.206.218.241:8080/seeyon/m3/apps/v5/biz/html/bizInfo.html?menuId=-5409992764838949920&name=%@",[[ZTUserInfoManage defaultService]getLoginUserName]]
        };
        //合同管理
        [ZTPrivatePushModule openSingleH5MethodsWithParaDic:dict];
    }else {
        [[[[UIApplication sharedApplication]keyWindow] rootViewController] showHUDOnlyText:@"请传输正确的功能模块的type类型"];
    }

}


#pragma mark- ------ 纯网址跳转需要单点登录 ------
+ (void)openSingleH5MethodsWithParaDic:(NSDictionary *)dict{
    
    ZTLoadZYHTMLViewController * html = [[ZTLoadZYHTMLViewController alloc] init];
    html.hostUrlString = dict[@"ip"];
    html.url = dict[@"url"];
    [(UINavigationController *)[[UIApplication sharedApplication]keyWindow].rootViewController pushViewController:html animated:YES];
}


+(void)ztnopermisspushVCTitle:(NSString *)title
{
    ZTNoPermissionsViewController *ztnopermiss = [[ZTNoPermissionsViewController alloc]init];
    ztnopermiss.navigationTitle = title;
    [(UINavigationController *)[[UIApplication sharedApplication]keyWindow].rootViewController pushViewController:ztnopermiss animated:YES];

}






/** 退出授权登录*/
+(void)logoutZTPrivate
{
    NSDictionary *dict = [[NSDictionary alloc]init];
    [[NSUserDefaults standardUserDefaults]setObject:dict forKey:ZTPrivateUserInfoKey];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
}

@end
